﻿using System;
using System.Collections.Generic;

namespace Pixels.Logic
{
    public class Colony // all data concerning the colony itself 
    {
        //public int numColonists;

        public HashSet<Colonist> allColonists = new HashSet<Colonist>();
        public HashSet<Colonist> idleColonists = new HashSet<Colonist>();
        public HashSet<Structure> allStructures = new HashSet<Structure>();
        public List<FenceSegment> allFenceSegments = new List<FenceSegment>();

        public ResourcePool accumulatedResources = new ResourcePool(allowNegatives: false);
        public int          totalLebensraum      = 0;
        public int          totalResearch        = 0;
        public int          totalDefense         = 0;

        public Colonist spawnColonist(Colonist.Occupation type) {
            var c = new Colonist(this, type);
            allColonists.Add(c);
            return c; 
        }
        public Structure spawnStructure(StructureType ofType, int x, int y) {
            this.calculateFenceSegments();
            var s = Structure.Spawn(ofType);
            s.myColony = this;
            s.OnTile = World.Map[x, y];
            allStructures.Add(s);
            return s;
        }

        public void destroyStructure(Structure s) {
            s.OnTile = null;
            this.allStructures.Remove(s);
        }

        public void removeColonist(Colonist c) {
            allColonists.Remove(c);
            idleColonists.Remove(c);
        }

        internal void reset() {            
            totalLebensraum = 0;
            totalResearch = 0;
            totalDefense  = 0;
        }
            
            // add custom events : 
            // post process turn

        internal void cleanup() {
            foreach (var s in allStructures) if (s.destroyed) s.OnTile = null;
            allStructures.RemoveWhere((s) => s.destroyed);
        }

        
        internal int countColonists(Colonist.Occupation occupation) {
            int counter = 0;
            foreach (var colonist in allColonists) if (colonist.MyOccupation == occupation) counter++;
            return counter;
        }

        void checkDefeat() {
            if (allColonists.Count == 0) defeat("All your colonists have died.");
            if (allStructures.Count == 0) defeat("All your structures have been overrrun");
        }

        void defeat(string reason) {
            //Ur.Debug.Log("DEFEAT!");
            Ur.Debug.Log(reason);
            var msg = Interface.Interface.createMessage(reason, true);
            
        }

        public void calculateFenceSegments() {

            World.Colony.allFenceSegments.Clear();

            foreach (var structure in World.Colony.allStructures) {
                var adjacent = structure.OnTile.getAdjacentTiles();
                foreach (var tt in adjacent)
                    if (!tt.HasStructure) World.Colony.allFenceSegments.Add(new FenceSegment(structure.OnTile, tt));
                    
            }
        }


    }




}
